package util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * ClassName:SqlSessionUtil
 * Package:util
 *
 * @date:2021/11/2 3:47 下午
 * @author: Tiangle
 */
public class SqlSessionUtil {
    private static SqlSession sqs;
    static {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqs = new SqlSessionFactoryBuilder().build(inputStream).openSession();
    }
    public static SqlSession getSqs(){
        return sqs;
    }
    public static<T> T getMapper(Class<T> clazz){
        T mapper = sqs.getMapper(clazz);
        return mapper;
    }
}
